<?php


namespace app\agent\controller;

class Agent extends AdminBase
{
	public function get_subordinate_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$options["uid"] = $this->admin_info["id"];
		$result = \app\agent\service\AgentService::get_subordinate_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function get_app_subordinate_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$options["uid"] = $this->admin_info["id"];
		$result = \app\agent\service\AgentService::get_app_subordinate_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function set_subordinate_authority()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["agent_id" => "require", "appid" => "require", "authority_config" => "require|array"], ["agent_id.require" => "agent_id 不能为空！", "appid.require" => "appid 不能为空！", "authority_config.require" => "authority_config 不能为空！"]);
		if (!$validate->check($options)) {
			$this->response(\app\agent\model\Code::fail, $validate->getError());
		}
		$options["uid"] = $this->admin_info["id"];
		$result = \app\agent\service\AgentService::set_subordinate_authority($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_subordinate_info($id, $appid)
	{
		$options = ["id" => $id, "appid" => $appid];
		$options["uid"] = $this->admin_info["id"];
		$result = \app\agent\service\AgentService::get_subordinate_info($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function subordinate_recharge()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require", "number" => "require|float|>=:0.01", "remarks" => "require"], ["id.require" => "id不能为空！", "number.require" => "充值数量不能为空！", "remarks.require" => "充值备注不能为空！"]);
		if (!$validate->check($options)) {
			$this->response(\app\agent\model\Code::fail, $validate->getError());
		}
		$options["uid"] = $this->admin_info["id"];
		$result = \app\agent\service\AgentService::subordinate_recharge($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function add_subordinate()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["account" => "require", "name" => "require", "password" => "require", "qq" => "require"], ["account.require" => "账号 不能为空", "name.require" => "昵称 不能为空", "password.require" => "密码 不能为空", "qq.require" => "qq 不能为空", "add_subordinate_authority.require" => "添加子代理权限 不能为空", "divide_subordinate_add_subordinate_authority.require" => "给子代理设置添加子代理权限 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\agent\model\Code::fail, $validate->getError());
		}
		$options["uid"] = $this->admin_info["id"];
		$result = \app\agent\service\AgentService::add_subordinate($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit_subordinate()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require"], ["id.require" => "id 不能为空", "add_subordinate_authority.require" => "添加子代理权限 不能为空", "divide_subordinate_add_subordinate_authority.require" => "给子代理设置添加子代理权限 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\agent\model\Code::fail, $validate->getError());
		}
		$options["uid"] = $this->admin_info["id"];
		$result = \app\agent\service\AgentService::edit_subordinate($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], $result["msg"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
}